package com.study.controller.PSIcontroller;

import java.util.Date;

import org.codehaus.jettison.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.study.model.mdao.PSImdao.PurchaseOrderMapper;
import com.study.model.pojo.PSIpojo.PurchaseOrder;
import com.study.model.pojo.PSIpojo.SupplierTab;
import com.study.service.PSIservice.PurchaseOrderServices;

@CrossOrigin
@RestController
public class PurchaseOrderController {
	@Autowired
	PurchaseOrderServices poServices;

	@Autowired
	PurchaseOrderMapper pum;

	/**
	 * 查询所有为被删除的订单
	 * @return
	 */	
	@RequestMapping("lectAll_purder")
	public String selectAll_purder() {
		System.out.println("查询所有没被删除的订单");
		return JSON.toJSONString(pum.findAllPurorder());
	}
	
	
	/**
	 * 新增一条订单
	 * @param purorder
	 * @return
	 */
	@PostMapping("/insert_one")
	public PurchaseOrder addone(PurchaseOrder purorder) {
		System.out.println("新增订单供货商编号：" + purorder.getSuppid());
		System.out.println(purorder.getRemark());
		PurchaseOrder po = new PurchaseOrder();
		SupplierTab st = new SupplierTab();
		po.setPurorderid("yweqyte");
		po.setPuropendate(new Date());
		/* po.setSupplierTab(purorder.getSupplierTab().getSuppid()); */
		po.setSettletype(purorder.getSettletype());
		po.setRemark(purorder.getRemark());
		String remark = poServices.addOne(po);
		System.out.println(remark);
		return po;
	}

	
	/**
	 * 修改订单状态为已审核
	 * @param param
	 * @return
	 */
	@PostMapping("/update_state")
	public String updatecgById(String param) {
		System.out.println("id:" + param);
		System.out.println("修改订单审核状态");
		poServices.updateYesState(param);
		System.out.println("修改成功");
		return JSON.toJSONString(pum.findAllPurorder());
	}

		
	/**
	 * 修改订单状态为3删除
	 * 
	 * @param purorderid
	 */
	@PostMapping("/update_scstate")
	public  String updatesbById(String param) {
		System.out.println(param);
		System.out.println("修改订单审核状态");
		poServices.updateNoState(param);
		System.out.println("删除成功");
		return JSON.toJSONString(pum.findAllPurorder());
	}
	
	/**
	 * 批量修改审核状态为2已审核
	 * @param id
	 */
	@RequestMapping("/update_MoreState")
	public void updateMoreState(@RequestParam("id")String id) {		
		try {
			JSONArray ja = new JSONArray(id);
			for (int i = 0; i < ja.length(); i++) {
				poServices.updateYesState(String.valueOf(ja.getJSONObject(i).get("id")));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 批量修改审核状态为3删除订单
	 * @param id
	 */
	@RequestMapping("/del_MoreState")
	public void delMoreState(@RequestParam("id")String id) {		
		try {
			JSONArray ja = new JSONArray(id);
			for (int i = 0; i < ja.length(); i++) {
				poServices.updateNoState(String.valueOf(ja.getJSONObject(i).get("id")));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
